package org.jeecg.modules.online.cgreport.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.lang.invoke.SerializedLambda;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecg.common.exception.TuringBootException;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.utils.CgformUtil;
import org.jeecg.modules.online.cgreport.entity.OnlCgreportHead;
import org.jeecg.modules.online.cgreport.entity.OnlCgreportItem;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportItemService;
import org.jeecg.modules.online.cgreport.service.IOnlCgreportParamService;
import org.jeecg.modules.online.config.p021c.OnlReportQueryBlackListHandler;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.export.ExcelExportServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service("onlCgreportAPIService")
/* renamed from: org.jeecg.modules.online.cgreport.service.a.b */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/cgreport/service/a/b.class */
public class OnlCgreportAPIService implements IOnlCgreportAPIService {

    /* renamed from: a */
    private static final Logger f514a = LoggerFactory.getLogger(OnlCgreportAPIService.class);
    @Autowired
    private OnlCgreportHeadServiceImpl onlCgreportHeadService;
    @Autowired
    private IOnlCgreportItemService onlCgreportItemService;
    @Autowired
    @Lazy
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private IOnlCgreportParamService onlCgreportParamService;
    @Autowired
    private OnlReportQueryBlackListHandler onlReportQueryBlackListHandler;

    /* renamed from: a */
//    private static /* synthetic */ Object m90a(SerializedLambda serializedLambda) {
//        String implMethodName = serializedLambda.getImplMethodName();
//        boolean z = true;
//        switch (implMethodName.hashCode()) {
//            case -982778162:
//                if (implMethodName.equals("getOrderNum")) {
//                    z = false;
//                    break;
//                }
//                break;
//            case -75622813:
//                if (implMethodName.equals("getCode")) {
//                    z = true;
//                    break;
//                }
//                break;
//            case 1731767923:
//                if (implMethodName.equals("getCgrheadId")) {
//                    z = true;
//                    break;
//                }
//                break;
//        }
//        switch (z) {
//            case false:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgreport/entity/OnlCgreportItem") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//                    return (v0) -> {
//                        return v0.getOrderNum();
//                    };
//                }
//                break;
//            case true:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgreport/entity/OnlCgreportItem") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getCgrheadId();
//                    };
//                }
//                break;
//            case true:
//                if (serializedLambda.getImplMethodKind() == 5 && serializedLambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && serializedLambda.getFunctionalInterfaceMethodName().equals("apply") && serializedLambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && serializedLambda.getImplClass().equals("org/jeecg/modules/online/cgreport/entity/OnlCgreportHead") && serializedLambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//                    return (v0) -> {
//                        return v0.getCode();
//                    };
//                }
//                break;
//        }
//        throw new IllegalArgumentException("Invalid lambda deserialization");
//    }

    @Override // org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService
    public Map<String, Object> getDataById(String id, Map<String, Object> params) {
        return getData(id, null, params);
    }

    @Override // org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService
    public Map<String, Object> getDataByCode(String code, Map<String, Object> params) {
        return getData(null, code, params);
    }

    @Override // org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService
    public Map<String, Object> getData(String id, String code, Map<String, Object> params) {
        OnlCgreportHead onlCgreportHead = null;
        if (oConvertUtils.isNotEmpty(id)) {
            onlCgreportHead = this.onlCgreportHeadService.getById(id);
        } else if (oConvertUtils.isNotEmpty(code)) {
        	LambdaQueryWrapper<OnlCgreportHead> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(OnlCgreportHead::getCode, code);
            onlCgreportHead = (OnlCgreportHead) this.onlCgreportHeadService.getOne(lambdaQueryWrapper);
        }
        if (onlCgreportHead == null) {
            throw new TuringBootException("实体不存在");
        }
        try {
            return executeSelectSqlRoute(onlCgreportHead.getDbSource(), onlCgreportHead.getCgrSql().trim(), params, onlCgreportHead.getId());
        } catch (Exception e) {
            f514a.error(e.getMessage(), e);
            throw new TuringBootException("SQL执行失败：" + e.getMessage());
        }
    }

    @Override // org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService
    public Map<String, Object> executeSelectSqlRoute(String dbKey, String sql, Map<String, Object> params, String headId) throws Exception {
        if (!this.onlReportQueryBlackListHandler.isPass(sql)) {
            throw new TuringBootException(this.onlReportQueryBlackListHandler.getError());
        }
        if (StringUtils.isNotBlank(dbKey)) {
            f514a.debug("Online报表: 走了多数据源逻辑");
            return this.onlCgreportHeadService.executeSelectSqlDynamic(dbKey, sql, params, headId);
        }
        f514a.debug("Online报表: 走了稳定逻辑");
        return this.onlCgreportHeadService.executeSelectSql(sql, headId, params);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v143, types: [java.util.List] */
    @Override // org.jeecg.modules.online.cgreport.service.IOnlCgreportAPIService
    public Workbook getReportWorkbook(String reportId, Map<String, Object> params) {
    	LambdaQueryWrapper<OnlCgreportItem> wrapper =  new LambdaQueryWrapper<OnlCgreportItem>().eq(OnlCgreportItem::getCgrheadId, reportId);
        wrapper.orderByAsc(OnlCgreportItem::getOrderNum);
        List<OnlCgreportItem> list = this.onlCgreportItemService.list(wrapper);
        ArrayList<ExcelExportEntity> arrayList = new ArrayList();
        Map<String,List<String>> hashMap = new HashMap(5);
        ArrayList<String> arrayList2 = new ArrayList();
        HashMap hashMap2 = new HashMap(5);
        for (OnlCgreportItem onlCgreportItem : list) {
            String fieldType = onlCgreportItem.getFieldType();
            String fieldName = onlCgreportItem.getFieldName();
            if ("1".equals(oConvertUtils.getString(onlCgreportItem.getIsShow()))) {
                ExcelExportEntity excelExportEntity = new ExcelExportEntity(onlCgreportItem.getFieldTxt(), fieldName, 15);
                m89a(onlCgreportItem, excelExportEntity);
                if ("date".equalsIgnoreCase(onlCgreportItem.getFieldType())) {
                    excelExportEntity.setFormat("yyyy-MM-dd");
                } else if ("datetime".equalsIgnoreCase(onlCgreportItem.getFieldType())) {
                    excelExportEntity.setFormat("yyyy-MM-dd HH:mm:ss");
                }
                String groupTitle = onlCgreportItem.getGroupTitle();
                if (oConvertUtils.isNotEmpty(groupTitle)) {
                    List arrayList3 = new ArrayList();
                    if (hashMap.containsKey(groupTitle)) {
                        arrayList3 = hashMap.get(groupTitle);
                        arrayList3.add(fieldName);
                    } else {
                        arrayList.add(new ExcelExportEntity(groupTitle, groupTitle, true));
                        arrayList3.add(fieldName);
                    }
                    hashMap.put(groupTitle, arrayList3);
                    excelExportEntity.setColspan(true);
                }
                if (oConvertUtils.isNotEmpty(fieldType) && oConvertUtils.isEmpty(onlCgreportItem.getDictCode()) && ("Integer".equals(fieldType) || "Long".equals(fieldType))) {
                    excelExportEntity.setType(4);
                }
                arrayList.add(excelExportEntity);
            }
            if ("1".equals(oConvertUtils.getString(onlCgreportItem.getIsTotal()))) {
                arrayList2.add(fieldName);
            }
        }
        for (Map.Entry entry : hashMap.entrySet()) {
            String str = (String) entry.getKey();
            List list2 = (List) entry.getValue();
            for (ExcelExportEntity excelExportEntity2 : arrayList) {
                if (str.equals(excelExportEntity2.getName()) && excelExportEntity2.isColspan()) {
                    excelExportEntity2.setSubColumnList(list2);
                }
            }
        }
        HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
        boolean z = true;
        Integer num = 1;
        params.put("pageSize", 10000);
        while (z) {
            Integer num2 = num;
            num = Integer.valueOf(num.intValue() + 1);
            params.put("pageNo", num2);
            List<Map<String,BigDecimal>> list3 = (List) getDataById(reportId, params).get("records");
            if (list3 == null || list3.size() == 0) {
                z = false;
            } else {
                if (arrayList2 != null && arrayList2.size() > 0) {
                    for (String str2 : arrayList2) {
                        BigDecimal bigDecimal = new BigDecimal(0.0d);
                        for (Map map : list3) {
                            String obj = map.get(str2).toString();
                            if (obj.matches("-?\\d+(.\\d+)?")) {
                                bigDecimal = bigDecimal.add(new BigDecimal(obj));
                            }
                        }
                        hashMap2.put(str2, bigDecimal);
                    }
                    list3.add(hashMap2);
                }
                new ExcelExportServer().createSheetForMap(hSSFWorkbook, new ExportParams(), arrayList, list3);
            }
        }
        return hSSFWorkbook;
    }

    /* renamed from: a */
    private void m89a(OnlCgreportItem onlCgreportItem, ExcelExportEntity excelExportEntity) {
        List<DictModel> queryColumnDictList = this.onlCgreportHeadService.queryColumnDictList(oConvertUtils.getString(onlCgreportItem.getDictCode()), null, null);
        if (queryColumnDictList != null && queryColumnDictList.size() > 0) {
            ArrayList arrayList = new ArrayList();
            for (DictModel dictModel : queryColumnDictList) {
                if (dictModel != null && dictModel.getValue() != null) {
                    if (dictModel.getValue().contains("_")) {
                        arrayList.add(dictModel.getText() + "_" + dictModel.getValue().replace("_", "---"));
                    } else {
                        arrayList.add(dictModel.getText() + "_" + dictModel.getValue());
                    }
                }
            }
            excelExportEntity.setReplace((String[]) arrayList.toArray(new String[arrayList.size()]));
        }
        String replaceVal = onlCgreportItem.getReplaceVal();
        if (oConvertUtils.isNotEmpty(replaceVal)) {
            excelExportEntity.setReplace(replaceVal.toString().split(CgformUtil.f233E));
        }
    }
}
